package com.fuershiye.woman.monitor.service.impl;

import java.util.List;

import com.fuershiye.woman.monitor.beans.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fuershiye.woman.monitor.dao.DepartmentDao;
import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.service.DepartmentService;
import com.google.common.collect.Lists;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
	

	//避免多次查询,请求一次后将数据缓存到内存中
	List<IdNameSname> allIdNameSname = null;
	List<IdAreaId> allIdAreaId = null;
//	List<IdNameSnameArea> allIdNameSnameArea = null;

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public void save(Department department) {
    	departmentDao.save(department);
		allIdNameSname = null;
		allIdAreaId = null;
//		allIdNameSnameArea = null;
    }

    @Override
    public void delete(Long id) {
    	departmentDao.delete(id);
    	allIdNameSname = null;
		allIdAreaId = null;
//		allIdNameSnameArea = null;
    }
    
    @Override
    public Department get(Long id) {

        return departmentDao.get(id);
    }
    
//    @Override
//    public IdNameSnameArea find(Long id) {
//    	if(allIdNameSnameArea==null){
////			list(String name, Long type, Integer level, Integer status, Page page)
//			List<Department> allDepartment = departmentDao.list("", null, null, 1, new Page(1, Integer.MAX_VALUE));
//    	}
//    	return find(id, allIdNameSname);
//    }

	@Override
	public IdNameSname find(Long id) {
		if(allIdNameSname==null){
			allIdNameSname = departmentDao.getAllSnames(null, null, null);
		}
		return find(id, allIdNameSname);
	}
    
    @Override
    public List<Department> list(String name, Long type, Integer level, Integer status, Page page) {
        return departmentDao.list(name, type, level, status, page);
    }

	@Override
	public List<Department> listByLevel(Integer level, Integer status) {
		return departmentDao.list("", null, level, status, null);
	}

    @Override
    public int getTotal(String name, Long type, Integer level, Integer status) {
    	return departmentDao.getTotal(name, type, level, status);
    }
    
    @Override
    public List<IdNameArea> listByPidOrArea(Long pid, Long area) {
    	return departmentDao.listByPidOrArea(pid, area);
    }
    
    @Override
    public Department findByAreaAndType(Long areaid, Long type) {
    	return departmentDao.findByAreaAndType(areaid, type);
    }

    @Override
    public Department findByUserName(String userName) {
        return departmentDao.findByUserName(userName);
    }

    @Override
    public List<IdNameSname> getAllSnames(Integer level, Long type, Integer status) {
        return departmentDao.getAllSnames(level, type, status);
    }
    
    @Override
    public List<IdNameSname> getAllSnames() {
    	if(allIdNameSname==null){
    		allIdNameSname = departmentDao.getAllSnames(null, null, null);
    	}
    	return allIdNameSname;
    }

	@Override
	public List<Long> getIdsByCondition(Integer level, int status) {
		return departmentDao.getIdsByCondition(null, level, null, null, status);
	}

	@Override
	public List<IdAreaId> getAllIdAreaId() {
		if(allIdAreaId==null){
			allIdAreaId = departmentDao.getAllIdAreaId(null, null);
		}
		return allIdAreaId;
	}
    
    @Override
    public List<IdNameSname> getAllByLevelAndArea(Integer level, Long areaid, int status) {
    	return departmentDao.getAllByLevelAndArea(level, areaid, status);
    }
    
    @Override
    public List<Long> getDepartIdsFromIdNameSname(List<IdNameSname> departmentList) {
		
		List<Long> departIds = Lists.newArrayList();
		
		if (departmentList.isEmpty()) {
			return departIds;
		}
		
		for (IdNameSname department : departmentList) {
			if (department != null) {
				departIds.add(department.getId());
			}
		}
		
		return departIds;
	}
    
    @Override
    public List<Long> getDepartIds(List<Department> departmentList) {

		List<Long> departIds = Lists.newArrayList();

		if (departmentList.isEmpty()) {
			return departIds;
		}

		for (Department department : departmentList) {
			if (department != null) {
				departIds.add(department.getId());
			}
		}

		return departIds;
	}
    
	private IdNameSname find(Long id, List<IdNameSname> allIdNameSname){
		
		if(allIdNameSname==null || id==null || id ==0 || allIdNameSname.size()<1){
			return null;
		}
		
		for (IdNameSname idNameSname : allIdNameSname) {
			if(idNameSname!=null && id.equals(idNameSname.getId())){
				return idNameSname;
			}
		}
		return null;
	}
	
}
